{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "817f86fd-ee90-4b89-86dd-1b018bed39b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1049008\n",
      "\\U001001b0\n"
     ]
    }
   ],
   "source": [
    "strs = \"􀆰\"\n",
    "# 用于返回给定字符的 Unicode 码点\n",
    "print(ord(strs))\n",
    "\"\"\"\n",
    "0：在数字前面填充零。\n",
    "8：总宽度为 8 个字符。\n",
    "x：将数字格式化为十六进制表示。\n",
    "\"\"\"\n",
    "print(f'\\\\U{ord(strs):08x}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ed7eca84-0478-4757-a6b0-2f536c989f0a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]􀆰陕西延长石􀆰油天然气有限责任公司,陕西延安716000;\n",
      "'[1]\\U001001b0陕西延长石\\U001001b0油天然气有限责任公司,陕西延安716000;'\n"
     ]
    }
   ],
   "source": [
    "text = \"[1]\\U001001b0陕西延长石\\U001001b0油天然气有限责任公司,陕西延安716000;\"\n",
    "print(text)\n",
    "# repr 函数 可以相当于 r'' 保留了无法编码的源数据\n",
    "text = repr(text)\n",
    "print(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b78d6747-cff8-45f0-8233-e82da33aaf48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "􃏬\n"
     ]
    }
   ],
   "source": [
    "import ast\n",
    "char = '\\\\U001033ec'\n",
    "# 将编码字符串装换成编码\n",
    "char = ast.literal_eval(f\"'{char}'\")\n",
    "print(char)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "594391fa-df9b-4212-8cd4-c6c646181c66",
   "metadata": {},
   "source": [
    "在Java中，\\uDBC0\\uDDB0 是一个UTF-16编码的代理对（surrogate pair），用于表示一个Unicode字符。每个部分分别代表一个16位的代码单元。\n",
    "\n",
    "\\uDBC0 是高代理项（high surrogate），其范围为0xD800到0xDBFF。\n",
    "\n",
    "\\uDDB0 是低代理项（low surrogate），其范围为0xDC00到0xDFFF。\n",
    "\n",
    "这两个代码单元组合在一起表示一个字符，其Unicode码点为 0x100B0（十六进制）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99304e61-f924-49fc-b230-ed582c3459cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将python的 Unicode 编码转换成 java 的 \n",
    "def unicode_to_utf16_surrogate_pair(char):\n",
    "    code_point = ord(char)  # 获取Unicode码点\n",
    "    if code_point >= 0x10000:\n",
    "        # 计算高代理和低代理\n",
    "        high = ((code_point - 0x10000) >> 10) + 0xD800\n",
    "        low = ((code_point - 0x10000) & 0x3FF) + 0xDC00\n",
    "        return (chr(high), chr(low))\n",
    "    else:\n",
    "        return (char,)  # 返回单个字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0da6f369-ceda-4ea1-bfd7-6f084fd5516b",
   "metadata": {},
   "outputs": [],
   "source": [
    "char = '\\\\U001033ec'\n",
    "# 将编码字符串装换成编码\n",
    "char = ast.literal_eval(f\"'{char}'\")\n",
    "surrogates = unicode_to_utf16_surrogate_pair(char)\n",
    "  h_v = orsurrogates[ga0]   l_v = osurrogatesrog[1    unicode_escape1 = f'\\\\u' + f'{h_v:'.upper()e\n",
    "    unicode_escape2 = f'\\\\u' + f'{l_v:04X}'.upper()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "242cc114-4385-4a3a-b457-42c4b68f0ce7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20013"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ord('中')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2f4b14f5-5eb4-400e-8d65-278c70e82767",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20013\n"
     ]
    }
   ],
   "source": [
    "# 假设 hex_string 是一个十六进制字符串\n",
    "hex_string = '4E2D'\n",
    "\n",
    "# 转换为十进制\n",
    "decimal_value = int(hex_string, 16)\n",
    "\n",
    "# 打印结果\n",
    "print(decimal_value)  # 输出: 20013"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7ec1b54-9147-4a9e-9c82-2e681127dac9",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "# 字符编码知识点"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41f6df8f-2465-44df-8329-9e58837374fa",
   "metadata": {},
   "source": [
    "### 1各个编码前缀"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2c57f8be-0373-4d42-bf14-58295f41d75d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 10 10 10\n"
     ]
    }
   ],
   "source": [
    "# 十进制\n",
    "dec = 10\n",
    "\n",
    "# 二进制\n",
    "bin_val = 0b1010  # 10 (十进制)\n",
    "\n",
    "# 八进制\n",
    "oct_val = 0o12    # 10 (十进制)\n",
    "\n",
    "# 十六进制\n",
    "hex_val = 0xA     # 10 (十进制)\n",
    "\n",
    "print(dec, bin_val, oct_val, hex_val)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8d48aa75-2dc1-45f8-aead-dd0f3c92f9e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n"
     ]
    }
   ],
   "source": [
    "# \\x 转义序列 用于表示单个字节的十六进制值。\n",
    "# \\x 后面跟随两个十六进制数字，表示一个字节的值（0-255）。\n",
    "print('\\x41')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "26a1d2af-e855-4b87-9015-3800279df753",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "中\n",
      "😀\n"
     ]
    }
   ],
   "source": [
    "# \\u 转义序列 用于表示一个 Unicode 字符的十六进制值。\n",
    "# \\u 后面跟随四个十六进制数字，表示一个字符的 Unicode 码点。\n",
    "# \\U：表示一个 Unicode 字符的完整码点，后跟八个数字，例如 \\U0001F600 表示表情符号\n",
    "print('\\u4E2D')\n",
    "print('\\U0001F600')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c13e29d2-5c1a-4fe0-ab6f-d7f2ff347051",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "65\n",
      "20013\n"
     ]
    }
   ],
   "source": [
    "# 获取字符 'A' 的 Unicode 码点\n",
    "code_point = ord('A')\n",
    "print(code_point)  # 输出: 65\n",
    "\n",
    "# 获取汉字 '中' 的 Unicode 码点\n",
    "code_point_chinese = ord('中')\n",
    "print(code_point_chinese)  # 输出: 20013"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ee930f3c-7c1e-472e-8382-03c252b3d884",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n",
      "中\n"
     ]
    }
   ],
   "source": [
    "# 将码点 65 转换为字符\n",
    "character = chr(65)\n",
    "print(character)  # 输出: 'A'\n",
    "\n",
    "# 将码点 20013 转换为汉字\n",
    "character_chinese = chr(20013)\n",
    "print(character_chinese)  # 输出: '中'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d66ca5c1-0492-4f80-80ec-213e652aaa88",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "十进制 10 转换为二进制: 0b1010\n",
      "十进制 10 转换为八进制: 0o12\n",
      "十进制 10 转换为十六进制: 0xa\n",
      "二进制 1010 转换为十进制: 10\n",
      "二进制 1010 转换为八进制: 0o12\n",
      "二进制 1010 转换为十六进制: 0xa\n",
      "八进制 12 转换为十进制: 10\n",
      "八进制 12 转换为二进制: 0b1010\n",
      "八进制 12 转换为十六进制: 0xa\n",
      "十六进制 a 转换为十进制: 10\n",
      "十六进制 a 转换为二进制: 0b1010\n",
      "十六进制 a 转换为八进制: 0o12\n"
     ]
    }
   ],
   "source": [
    "def decimal_to_binary(decimal):\n",
    "    # 将十进制数转换为二进制\n",
    "    return bin(decimal)\n",
    "\n",
    "def decimal_to_octal(decimal):\n",
    "    # 将十进制数转换为八进制\n",
    "    return oct(decimal)\n",
    "\n",
    "def decimal_to_hexadecimal(decimal):\n",
    "    # 将十进制数转换为十六进制\n",
    "    return hex(decimal)\n",
    "\n",
    "def binary_to_decimal(binary_string):\n",
    "    # 将二进制字符串转换为十进制\n",
    "    return int(binary_string, 2)\n",
    "\n",
    "def binary_to_octal(binary_string):\n",
    "    # 将二进制字符串转换为八进制\n",
    "    return oct(int(binary_string, 2))\n",
    "\n",
    "def binary_to_hexadecimal(binary_string):\n",
    "    # 将二进制字符串转换为十六进制\n",
    "    return hex(int(binary_string, 2))\n",
    "\n",
    "def octal_to_decimal(octal_string):\n",
    "    # 将八进制字符串转换为十进制\n",
    "    return int(octal_string, 8)\n",
    "\n",
    "def octal_to_binary(octal_string):\n",
    "    # 将八进制字符串转换为二进制\n",
    "    return bin(int(octal_string, 8))\n",
    "\n",
    "def octal_to_hexadecimal(octal_string):\n",
    "    # 将八进制字符串转换为十六进制\n",
    "    return hex(int(octal_string, 8))\n",
    "\n",
    "def hexadecimal_to_decimal(hex_string):\n",
    "    # 将十六进制字符串转换为十进制\n",
    "    return int(hex_string, 16)\n",
    "\n",
    "def hexadecimal_to_binary(hex_string):\n",
    "    # 将十六进制字符串转换为二进制\n",
    "    return bin(int(hex_string, 16))\n",
    "\n",
    "def hexadecimal_to_octal(hex_string):\n",
    "    # 将十六进制字符串转换为八进制\n",
    "    return oct(int(hex_string, 16))\n",
    "\n",
    "# 示例值\n",
    "decimal_value = 10\n",
    "binary_value = '1010'\n",
    "octal_value = '12'\n",
    "hexadecimal_value = 'a'\n",
    "\n",
    "# 打印转换结果\n",
    "print(f\"十进制 {decimal_value} 转换为二进制: {decimal_to_binary(decimal_value)}\")\n",
    "print(f\"十进制 {decimal_value} 转换为八进制: {decimal_to_octal(decimal_value)}\")\n",
    "print(f\"十进制 {decimal_value} 转换为十六进制: {decimal_to_hexadecimal(decimal_value)}\")\n",
    "\n",
    "print(f\"二进制 {binary_value} 转换为十进制: {binary_to_decimal(binary_value)}\")\n",
    "print(f\"二进制 {binary_value} 转换为八进制: {binary_to_octal(binary_value)}\")\n",
    "print(f\"二进制 {binary_value} 转换为十六进制: {binary_to_hexadecimal(binary_value)}\")\n",
    "\n",
    "print(f\"八进制 {octal_value} 转换为十进制: {octal_to_decimal(octal_value)}\")\n",
    "print(f\"八进制 {octal_value} 转换为二进制: {octal_to_binary(octal_value)}\")\n",
    "print(f\"八进制 {octal_value} 转换为十六进制: {octal_to_hexadecimal(octal_value)}\")\n",
    "\n",
    "print(f\"十六进制 {hexadecimal_value} 转换为十进制: {hexadecimal_to_decimal(hexadecimal_value)}\")\n",
    "print(f\"十六进制 {hexadecimal_value} 转换为二进制: {hexadecimal_to_binary(hexadecimal_value)}\")\n",
    "print(f\"十六进制 {hexadecimal_value} 转换为八进制: {hexadecimal_to_octal(hexadecimal_value)}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "928492dd-18fa-4e3c-8603-1b0b57361fa5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "中文字符 '中' 的二进制表示: 11100100 10111000 10101101\n"
     ]
    }
   ],
   "source": [
    "def chinese_to_binary(chinese_string):\n",
    "    # 将中文字符串编码为 UTF-8 字节\n",
    "    byte_array = chinese_string.encode('utf-8')\n",
    "    # 将每个字节转换为二进制字符串，并去掉前缀 '0b'\n",
    "    binary_strings = [format(byte, '08b') for byte in byte_array]\n",
    "    # 将二进制字符串连接起来\n",
    "    return ' '.join(binary_strings)\n",
    "\n",
    "# 示例\n",
    "chinese_character = '中'\n",
    "binary_representation = chinese_to_binary(chinese_character)\n",
    "print(f\"中文字符 '{chinese_character}' 的二进制表示: {binary_representation}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17eeb7e7-2cf7-441f-9b81-3fd3e260842e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
